package com.tanhua.manage.service;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tanhua.manage.mapper.RoleMapper;
import com.tanhua.manage.mapper.RoleOpreationMapper;
import com.tanhua.manage.pojo.RoleOpreation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 杨一帆
 * @date 2020/12/13  14:46
 */
@Service
public class RoleOpreationService extends ServiceImpl<RoleOpreationMapper, RoleOpreation> {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleOpreationMapper roleOpreationMapper;

    public IPage<RoleOpreation> findByPage(Integer page, Integer pageSize, String role, String opreation, Long startTime, Long endTime) {

        Page<RoleOpreation> infoPage = new Page<>(page, pageSize);
        //设置排序
        infoPage.setDesc("creat_time");
        LambdaQueryWrapper<RoleOpreation> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotEmpty(role)) {
            lambdaQueryWrapper.like(RoleOpreation::getRole, role);
        }
        if (startTime != null && endTime != null && startTime.equals(endTime)) {
            // 当 开始时间 和 结束时间 为相同时，即同一天。结束时间+1天毫秒数
            endTime = startTime + 86400000;
        }
        if (startTime != null && startTime > 0) {
            lambdaQueryWrapper.ge(RoleOpreation::getCreatTime, startTime);
        }
        if (endTime != null && endTime > 0) {
            lambdaQueryWrapper.le(RoleOpreation::getCreatTime, startTime);
        }
        if (StrUtil.isNotEmpty(opreation)) {
            lambdaQueryWrapper.like(RoleOpreation::getOpreation, opreation);
        }
        return super.getBaseMapper().selectPage(infoPage, lambdaQueryWrapper);
    }

    public Map<String, String> qureyOne(Integer id) {
        RoleOpreation one = super.getOne(Wrappers.<RoleOpreation>lambdaQuery().eq(RoleOpreation::getId, id));

        String role = one.getRole();
        String s = roleMapper.findRole(role);


        Map<String, String> map = new HashMap<>();

        //角色信息
        map.put("role", role + "(" + s + ")");
        map.put("opreation", one.getOpreation());
        map.put("describe", one.getDescribes());
        map.put("ip", one.getIp());
        Long time = one.getCreatTime();
        DateTime dateTime = new DateTime(time);
        map.put("time", DateUtil.format(dateTime,"yyyy-MM-dd"));
        return map;
    }

    /**
     * 插入数据
     * @param roleOpreation
     */
    public void saveRoleOpreation(RoleOpreation roleOpreation){
        try {
            //设置本机Ip
            String hostAddress = InetAddress.getLocalHost().getHostAddress();
            roleOpreation.setIp(hostAddress);
        } catch (UnknownHostException e) {
            throw new RuntimeException("未获取到IP地址"+e.getMessage(),e);
        }
        //设置时间
        roleOpreation.setCreatTime(System.currentTimeMillis());



        roleOpreationMapper.insert(roleOpreation);
    }
}
